package com.redpack.quick.service.impl;

import com.redpack.quick.common.beans.UserTaskBean;
import com.redpack.quick.datasource.dao.CashHistoryDao;
import com.redpack.quick.datasource.dao.TaskDao;
import com.redpack.quick.datasource.dao.UserInfoDao;
import com.redpack.quick.datasource.dao.UserTaskInfoDao;
import com.redpack.quick.datasource.entity.CashHistoryEntity;
import com.redpack.quick.datasource.entity.UserInfoEntity;
import com.redpack.quick.datasource.entity.UserTaskInfoEntity;
import com.redpack.quick.service.IUserTaskInfo;
import com.redpack.quick.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by shuttle on 4/4/18.
 */
@Service
public class UserTaskInfoImpl implements IUserTaskInfo {

    @Autowired
    UserTaskInfoDao userTaskInfoDao;

    @Autowired
    UserInfoDao userInfoDao;

    @Autowired
    TaskDao taskDao;

    @Override
    public Page<UserTaskBean> findByOpenId(String uid, int pageSize, int pageNum) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = new PageRequest(pageNum, pageSize, sort);
        Page<UserTaskInfoEntity> taskList = userTaskInfoDao.findByUid(uid, pageable);
        List<UserTaskBean> userTaskBeans = new ArrayList<>();
        taskList.getContent().forEach(item -> {
            UserTaskBean bean = new UserTaskBean();
            bean.setCreateTime(DateUtils.formatTimeStampToString(item.getCreateTime()));
            bean.setPrice(item.getPrice());
            bean.setStatus(statusValue(item.getStatus()));
            bean.setTaskName(taskDao.findOne(Long.parseLong(item.getTaskId())).getName());
            userTaskBeans.add(bean);
        });
        Page<UserTaskBean> taskBeanPage = new PageImpl<UserTaskBean>(userTaskBeans, pageable, taskList.getTotalElements());
        return taskBeanPage;
    }

    @Override
    public UserTaskInfoEntity save(UserTaskInfoEntity entity) {
        String uid = entity.getUid();
        String taskId = entity.getTaskId();
        if(findByTaskIdAndUid(uid, taskId).equals("y")){//如果用户已经存在该任务且未完成不结算
            return null;
        }else{//新增任务的金额直接增加到用户的任务金额中，当任务计算时，将金额提取到用户获取总额中，用户取现后，从总额中减去取现金额
            UserInfoEntity user = userInfoDao.findOne(Long.parseLong(uid));
            if(user != null){
                BigDecimal t = user.getTaskMoney();
                BigDecimal p = new BigDecimal(entity.getPrice());
                t = t.add(p);
                user.setTaskMoney(t);
            }
            userInfoDao.save(user);
        }
        return userTaskInfoDao.save(entity);
    }

    @Override
    public String findByTaskIdAndUid(String uid, String taskId){
        return userTaskInfoDao.findByUidAndTaskIdAndStatusNot(uid, taskId, "30").size() == 0 ? "n":"y";
    }

    private String statusValue(String status){
        if("20".equals(status)){
            return "审核中";
        }else if("30".equals(status)){
            return "已结算";
        }else{
            return "进行中";
        }
    }
}
